package com.weihuachao.https;


import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.weihuachao.entity.Work;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Virgo on 2016/7/30.
 */
public abstract class BasicRequestCallBck<T> extends RequestCallBack<String> {
    private Type type;

    public BasicRequestCallBck() {
        Type superClass = this.getClass().getGenericSuperclass();
        this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
    }

    @Override
    public void onSuccess(ResponseInfo<String> responseInfo) {
        if (responseInfo != null) {
            String json = responseInfo.result;
            T t = JSON.parseObject(json, type);
            if (t != null) {
                success(t);
            } else {
                XUtils.show("解析失败");
            }

        } else {
            XUtils.show("数据加载失败");
        }
    }

    @Override
    public void onFailure(HttpException e, String s) {
        XUtils.show("网络请求失败");
    }

    public abstract void success(T data);
}
